package services

import (
	"encoding/json"
	"gorm.io/gorm"
	"mygo/app/models"
	"mygo/app/tools"
	"strings"
)

func HandelPyja(forecast *models.Forecast, time string, db *gorm.DB) {
	timeArr := strings.Split(time, "-")
	year := timeArr[0]
	month := timeArr[1]
	day := timeArr[2]
	for i, report := range forecast.Reports {
		if i == 1 {
			for j := 0; j < 2; j++ {
				y, m, d := tools.GetNextDay(year, month, day, j+1)
				t := y + "-" + m + "-" + d
				var tideAj models.TwcTide
				if db.Where("date = ? AND station = ?", t, "鳌江").First(&tideAj).RowsAffected > 0 {
					if tideAj.TimeHigh1 != "" {
						//report.Data[4][4*j+2].Value = tideAj.TimeHigh1
						tArr := strings.Split(tideAj.TimeHigh1, ":")
						report.Data[4][4*j+2].Value = tArr[0] + "点" + tArr[1] + "分"
						report.Data[2][4*j+2].Value = tools.Float64ToFixedStrZ(tools.StrToFloat64(tideAj.TideHigh1)/100, 2, "0.00") + "米"
						report.Data[3][4*j+2].Value = tools.Float64ToFixedStrZ(tools.StrToFloat64(tideAj.TideHigh1)/100+3.19, 2, "0.00") + "米"
					}
					if tideAj.TimeLow1 != "" {
						//report.Data[4][4*j+3].Value = tideAj.TimeLow1
						tArr := strings.Split(tideAj.TimeLow1, ":")
						report.Data[4][4*j+3].Value = tArr[0] + "点" + tArr[1] + "分"
						report.Data[2][4*j+3].Value = tools.Float64ToFixedStrZ(tools.StrToFloat64(tideAj.TideLow1)/100, 2, "0.00") + "米"
						report.Data[3][4*j+3].Value = tools.Float64ToFixedStrZ(tools.StrToFloat64(tideAj.TideLow1)/100+3.19, 2, "0.00") + "米"
					}
					if tideAj.TimeHigh2 != "" {
						//report.Data[4][4*j+4].Value = tideAj.TimeHigh2
						tArr := strings.Split(tideAj.TimeHigh2, ":")
						report.Data[4][4*j+4].Value = tArr[0] + "点" + tArr[1] + "分"
						report.Data[2][4*j+4].Value = tools.Float64ToFixedStrZ(tools.StrToFloat64(tideAj.TideHigh2)/100, 2, "0.00") + "米"
						report.Data[3][4*j+4].Value = tools.Float64ToFixedStrZ(tools.StrToFloat64(tideAj.TideHigh2)/100+3.19, 2, "0.00") + "米"
					}
					if tideAj.TimeLow2 != "" {
						//report.Data[4][4*j+5].Value = tideAj.TimeLow2
						tArr := strings.Split(tideAj.TimeLow2, ":")
						report.Data[4][4*j+5].Value = tArr[0] + "点" + tArr[1] + "分"
						report.Data[2][4*j+5].Value = tools.Float64ToFixedStrZ(tools.StrToFloat64(tideAj.TideLow2)/100, 2, "0.00") + "米"
						report.Data[3][4*j+5].Value = tools.Float64ToFixedStrZ(tools.StrToFloat64(tideAj.TideLow2)/100+3.19, 2, "0.00") + "米"
					}
				}
				var tideNj models.TwcTide
				if db.Where("date = ? AND station = ?", t, "南麂").First(&tideNj).RowsAffected > 0 {
					if tideNj.TimeHigh1 != "" {
						//report.Data[7][4*j+2].Value = tideNj.TimeHigh1
						tArr := strings.Split(tideNj.TimeHigh1, ":")
						report.Data[7][4*j+2].Value = tArr[0] + "点" + tArr[1] + "分"
						report.Data[5][4*j+2].Value = tools.Float64ToFixedStrZ(tools.StrToFloat64(tideNj.TideHigh1)/100, 2, "0.00") + "米"
						report.Data[6][4*j+2].Value = tools.Float64ToFixedStrZ(tools.StrToFloat64(tideNj.TideHigh1)/100+1.71, 2, "0.00") + "米"
					}
					if tideNj.TimeLow1 != "" {
						//report.Data[7][4*j+3].Value = tideNj.TimeLow1
						tArr := strings.Split(tideNj.TimeLow1, ":")
						report.Data[7][4*j+3].Value = tArr[0] + "点" + tArr[1] + "分"
						report.Data[5][4*j+3].Value = tools.Float64ToFixedStrZ(tools.StrToFloat64(tideNj.TideLow1)/100, 2, "0.00") + "米"
						report.Data[6][4*j+3].Value = tools.Float64ToFixedStrZ(tools.StrToFloat64(tideNj.TideLow1)/100+1.71, 2, "0.00") + "米"
					}
					if tideNj.TimeHigh2 != "" {
						//report.Data[7][4*j+4].Value = tideNj.TimeHigh2
						tArr := strings.Split(tideNj.TimeHigh2, ":")
						report.Data[7][4*j+4].Value = tArr[0] + "点" + tArr[1] + "分"
						report.Data[5][4*j+4].Value = tools.Float64ToFixedStrZ(tools.StrToFloat64(tideNj.TideHigh2)/100, 2, "0.00") + "米"
						report.Data[6][4*j+4].Value = tools.Float64ToFixedStrZ(tools.StrToFloat64(tideNj.TideHigh2)/100+1.71, 2, "0.00") + "米"
					}
					if tideNj.TimeLow2 != "" {
						//report.Data[7][4*j+5].Value = tideNj.TimeLow2
						tArr := strings.Split(tideNj.TimeLow2, ":")
						report.Data[7][4*j+5].Value = tArr[0] + "点" + tArr[1] + "分"
						report.Data[5][4*j+5].Value = tools.Float64ToFixedStrZ(tools.StrToFloat64(tideNj.TideLow2)/100, 2, "0.00") + "米"
						report.Data[6][4*j+5].Value = tools.Float64ToFixedStrZ(tools.StrToFloat64(tideNj.TideLow2)/100+1.71, 2, "0.00") + "米"
					}
				}
			}
		} else {

			foreListPyja := GetForeListByCode("pyja", models.Db)

			var foreDetailPyja models.ForeDetailJson
			if models.Db.Where("foreListId = ? AND state != ?", foreListPyja.Id, "0").Order("time desc").First(&foreDetailPyja).RowsAffected != 0 {
				var wzhyData = models.Forecast{}
				json.Unmarshal(foreDetailPyja.Data, &wzhyData)
				//for j, v := range wzhyData.Reports[1].Data {
				//	report.Data[j][3].Value = v[3].Value
				//}
				report.Data[4][1].Value = wzhyData.Reports[0].Data[4][1].Value
				report.Data[4][5].Value = wzhyData.Reports[0].Data[4][5].Value
			} else {
				report.Data[4][1].Value = "海温℃"
				report.Data[4][5].Value = "海温℃"
			}

			var foreList models.ForeList
			db.Where("code = ?", "rayg").First(&foreList)
			var foreDetail models.ForeDetailJson
			db.Where("foreListId = ? AND time = ? AND state != ?", foreList.Id, time, "0").First(&foreDetail)
			var raData = models.Forecast{}
			json.Unmarshal(foreDetail.Data, &raData)
			if len(raData.Reports) == 0 {
				continue
			}

			type item struct {
				raRow  int
				raCol  int
				pyRow  int
				pyCol  int
				offset float64
			}
			items := []item{
				{raRow: 4, raCol: 4, pyRow: 2, pyCol: 3, offset: -0.1},
				{raRow: 5, raCol: 4, pyRow: 2, pyCol: 7, offset: -0.1},
				{raRow: 1, raCol: 4, pyRow: 3, pyCol: 3, offset: -0.2},
				{raRow: 2, raCol: 4, pyRow: 3, pyCol: 7, offset: -0.2},
			}

			for _, v := range items {
				waveValue := raData.Reports[1].Data[v.raRow][v.raCol].Value
				waveValueArr := tools.GetNumFloat64(waveValue)
				report.Data[v.pyRow][v.pyCol+1].Value = raData.Reports[1].Data[v.raRow][v.raCol+2].Value
				if len(waveValueArr) == 2 {
					str := strings.Split(strings.Split(waveValue, "到")[0], "，")[1]
					report.Data[v.pyRow][v.pyCol].Value = tools.Float64ToFixedStr(waveValueArr[0]+v.offset, 1) + "-" + tools.Float64ToFixedStr(waveValueArr[0], 1) + "米，" + str + "到" + tools.Float64ToFixedStr(waveValueArr[1]+v.offset, 1) + "-" + tools.Float64ToFixedStr(waveValueArr[1], 1) + "米"
				} else if len(waveValueArr) == 1 {
					report.Data[v.pyRow][v.pyCol].Value = tools.Float64ToFixedStr(waveValueArr[0]+v.offset, 1) + "-" + tools.Float64ToFixedStr(waveValueArr[0], 1) + "米"
				}
			}

		}
	}
}
